package cc.rengu.igas.bomp.biz.service.impl;

import cc.rengu.igas.bomp.common.enums.RespCodeEnum;
import cc.rengu.igas.bomp.facade.BompUserManageService;
import cc.rengu.igas.bomp.facade.request.UserInfoManageRequest;
import cc.rengu.igas.bomp.facade.request.UserInfoQueryRequest;
import cc.rengu.igas.bomp.facade.request.UserLimitManageRequest;
import cc.rengu.igas.bomp.facade.request.UserTxnLimitCfgQueryRequest;
import cc.rengu.igas.bomp.facade.response.UserInfoManageResponse;
import cc.rengu.igas.bomp.facade.response.UserInfoQueryResponse;
import cc.rengu.igas.bomp.facade.response.UserLimitManageResponse;
import cc.rengu.igas.bomp.facade.response.UserTxnLimitCfgQueryResponse;
import cc.rengu.igas.bomp.facade.result.Result;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.DubboResult;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;
import com.alibaba.fastjson.JSONObject;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;

public class BompUserManageServiceImpl implements BompUserManageService {

    private final RgLogger rglog = RgLog.getLogger(this.getClass().getName());

    @Override
    public Result<UserInfoQueryResponse> userInfoQuery(UserInfoQueryRequest request) {
        DubboService dubboService = new DubboServiceImpl();
        Result<UserInfoQueryResponse> result = new Result<>();
        UserInfoQueryResponse response = new UserInfoQueryResponse();
        try {
            rglog.debug("请求参数:<{}>", JSONObject.toJSONString(request));
            DubboResult dubboResult = dubboService.callBizService(request, UserInfoQueryResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            response = (UserInfoQueryResponse) dubboResult.getResult();
            response.setRespCode(dubboResult.getRespCode());
            response.setRespDesc(dubboResult.getRespDesc());
            result.setResult(response);
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("用户信息查询失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                response.setRespCode(((BizException) e).getCode());
                response.setRespDesc(e.getMessage());
            } else {
                response.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                response.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
            result.setResult(response);
        }
        return result;
    }

    @Override
    public Result<UserInfoManageResponse> userInfoManage(UserInfoManageRequest request) {
        DubboService dubboService = new DubboServiceImpl();
        Result<UserInfoManageResponse> result = new Result<>();
        UserInfoManageResponse response = new UserInfoManageResponse();
        try {
            rglog.debug("请求参数:<{}>", JSONObject.toJSONString(request));
            DubboResult dubboResult = dubboService.callBizService(request, UserInfoManageResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            response = (UserInfoManageResponse) dubboResult.getResult();
            response.setRespCode(dubboResult.getRespCode());
            response.setRespDesc(dubboResult.getRespDesc());
            result.setResult(response);
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("用户信息管理失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                response.setRespCode(((BizException) e).getCode());
                response.setRespDesc(e.getMessage());
            } else {
                response.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                response.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
            result.setResult(response);
        }
        return result;
    }

    @Override
    public Result<UserTxnLimitCfgQueryResponse> userTxnLimitCfgQuery(UserTxnLimitCfgQueryRequest request) {
        DubboService dubboService = new DubboServiceImpl();
        Result<UserTxnLimitCfgQueryResponse> result = new Result<>();
        UserTxnLimitCfgQueryResponse response = new UserTxnLimitCfgQueryResponse();
        try {
            rglog.debug("请求参数:<{}>", JSONObject.toJSONString(request));
            DubboResult dubboResult = dubboService.callBizService(request, UserTxnLimitCfgQueryResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            response = (UserTxnLimitCfgQueryResponse) dubboResult.getResult();
            response.setRespCode(dubboResult.getRespCode());
            response.setRespDesc(dubboResult.getRespDesc());
            result.setResult(response);
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("用户限额配置信息查询失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                response.setRespCode(((BizException) e).getCode());
                response.setRespDesc(e.getMessage());
            } else {
                response.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                response.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
            result.setResult(response);
        }
        return result;
    }

    @Override
    public Result<UserLimitManageResponse> userLimitManage(UserLimitManageRequest request) {
        DubboService dubboService = new DubboServiceImpl();
        Result<UserLimitManageResponse> result = new Result<>();
        UserLimitManageResponse response = new UserLimitManageResponse();
        try {
            rglog.debug("请求参数:<{}>", JSONObject.toJSONString(request));
            DubboResult dubboResult = dubboService.callBizService(request, UserLimitManageResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            response = (UserLimitManageResponse) dubboResult.getResult();
            response.setRespCode(dubboResult.getRespCode());
            response.setRespDesc(dubboResult.getRespDesc());
            result.setResult(response);
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("用户限额管理失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                response.setRespCode(((BizException) e).getCode());
                response.setRespDesc(e.getMessage());
            } else {
                response.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                response.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
            result.setResult(response);
        }
        return result;
    }
}
